//
//  Link.cpp
//  RoutingEvaluator
//
//  Created by Christopher Kemsley on 4/15/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include "Link.hpp"
#import "Node.hpp"

NamespaceBegin






// Will create this link (and update the links() property of the appropriate Nodes)
Link*	Link :: createLink	( LinkInfo* identity , NodeSet* allNodes )
	{
	Node * n1	=	allNodes -> nodeWithNodeInfo(identity->endpoint1) ;
	Node * n2	=	allNodes -> nodeWithNodeInfo(identity->endpoint2) ;
	if ( n1==0 || n2 == 0 )
		ThrowException ( "Link" , "Tried to create link, but one endpoint doesn't exist" , __FILE__ , __LINE__ ) ;
	return new Link ( identity , n1 , n2 ) ;
	}
Link*	Link :: createLink	( LinkInfo* identity , Node* n1 , Node* n2 )
	{
	if ( n1==0 || n2 == 0 )
		ThrowException ( "Link" , "Tried to create link, but one endpoint is nil" , __FILE__ , __LINE__ ) ;
	return new Link ( identity , n1 , n2 ) ;
	}

// Will create all links whose cost is less than maxCost
std::vector<Link*>	Link :: createLinks	( LinkInfoSet* costs , NodeSet* allNodes , Float64 maxRange )
	{
	std::vector<Link*>	links ;
	links . reserve ( costs->links().size() ) ;
	
	std::vector<LinkInfo*>::const_iterator	here	=	costs->links().begin() ;
	std::vector<LinkInfo*>::const_iterator	end		=	costs->links().end() ;
	while ( here < end )
		{
		LinkInfo * cost = *(here++) ;
		if ( cost->distance() <= maxRange )
			links . push_back ( createLink(cost,allNodes) ) ;
		}
	
	return links ;
	}
Node*	Link :: nodeOppositeFrom ( Node * from )
	{
	if ( from == endpoint1 )
		return endpoint2 ;
	if  ( from == endpoint2 )
		return endpoint1 ;
	return 0 ;
	}



Link :: Link ( LinkInfo* inID , Node* node1 , Node* node2 )
:	identity	( inID )
,	endpoint1	( node1 )
,	endpoint2	( node2 )
	{
	endpoint1 -> mLinks . push_back(this) ;
	endpoint2 -> mLinks . push_back(this) ;
	}



NamespaceEnd
